package com.hunter.api.help;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

/**
 * DateHelper
 * 日期时间相关方法
 *
 * @author CheneyZhao
 * @version 1.0
 * @date 2018/9/12 11:26
 */
public class DateHelper {

    /**
     * 获取当前UTC时间戳：秒数
     *
     * @return 秒数
     */
    public static Long currentTimeSeconds() {
        return currentTimeMillisecond() / 1000L;
    }

    /**
     * 获取当前UTC时间戳：毫秒数
     *
     * @return 毫秒数
     */
    public static Long currentTimeMillisecond() {
        Calendar cal = Calendar.getInstance();
        return cal.getTimeInMillis();
    }

    /**
     * 获取当前timestamp
     *
     * @return 时间戳
     */
    public static Timestamp currentTimestamp() {
        //获取当前毫秒数：System.currentTimeMillis() 而不是new Date().getTime()
        return timeToTimestamp(currentTimeMillisecond());
    }

    public static Timestamp timeToTimestamp(long time) {
        return new Timestamp(time);
    }

    /**
     * 秒数 转 *天*小时*分*秒
     *
     * @param seconds 秒数
     * @return *天*小时*分*秒
     */
    public static String getTimeStr(int seconds) {
        int day = seconds / (3600 * 24);
        int hour = (seconds % (3600 * 24)) / 3600;
        int minute = (seconds % 3600) / 60;
        int second = seconds % 60;

        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day).append("天");
            if (hour == 0 && minute == 0 && second == 0) {
                //后面忽略
            } else if (second > 0) {
                sb.append(hour).append("小时").append(minute).append("分").append(second).append("秒");
            } else if (minute > 0) {
                sb.append(hour).append("小时").append(minute).append("分");
            } else {
                sb.append(hour).append("小时");
            }
        } else if (hour > 0) {
            sb.append(hour).append("小时");
            if (minute == 0 && second == 0) {
                //后面忽略
            } else if (second > 0) {
                sb.append(minute).append("分").append(second).append("秒");
            } else {
                sb.append(minute).append("分");
            }
        } else if (minute > 0) {
            sb.append(minute).append("分");
            if (second > 0) {
                sb.append(second).append("秒");
            }
        } else {
            sb.append(second).append("秒");
        }

        return sb.toString();
    }

    /**
     * 根据本地时区，判断是否是晚上？
     * @return 晚上返回true
     */
    public static boolean isNight() {
        int hour = DateTime.now(DateTimeZone.getDefault()).getHourOfDay();
        return (hour > 18 || hour < 8);
    }

    public static String getTimeZone(){
        Calendar cal = Calendar.getInstance();
        int offset = cal.get(Calendar.ZONE_OFFSET);
        cal.add(Calendar.MILLISECOND, -offset);
        Long timeStampUTC = cal.getTimeInMillis();
        Long timeStamp = System.currentTimeMillis();
        Long timeZone = (timeStamp - timeStampUTC) / (1000 * 3600); System.out.println(timeZone.intValue());
        return String.valueOf(timeZone+1);
    }

    public static String dateToString(String pattern) {
        Date date = new Date(currentTimeMillisecond());
        return dateToString(date, pattern);
    }

    /**
     * 获取指定UTC时间戳：秒数
     *
     * @return 秒数
     */
    public static Long currentTimeSeconds(String stringTime){
        // 获取某个时间格式的时间戳
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(stringTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();

        // 如果想获取的时间戳为13位那就不用除1000
        ts = ts / 1000;
        System.out.println("时间戳为："+ts);
        return ts;
    }


    /**
     * 获取指定UTC时间戳：秒数
     *
     * @return 秒数
     */
    public static Long currentTimeSeconds(Date date){
        long ts = date.getTime();
        // 如果想获取的时间戳为13位那就不用除1000
        ts = ts / 1000;
        System.out.println("时间戳为："+ts);
        return ts;
    }

    /**
     * 获得今天开始时间
     * */
    public static Date getDayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获得今天结束时间
     * */
    public static Date getDayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    /**
     * 获得本周开始时间
     * */
    public static Date getWeekStartDay() {
        Calendar toweekStart = Calendar.getInstance();
        int day_of_week = toweekStart.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
            day_of_week = 7 ;
        }
        toweekStart.add(Calendar.DATE , -day_of_week + 1 );
        //toweekStart.add(Calendar.DATE, 0);
        toweekStart.set(Calendar.HOUR_OF_DAY, 0);
        toweekStart.set(Calendar.MINUTE, 0);
        toweekStart.set(Calendar.SECOND, 0);
        toweekStart.set(Calendar.MILLISECOND, 0);
        return toweekStart.getTime();
    }

    /**
     * 获得本周结束时间
     * */
    public static Date getWeekEndDay() {
        Calendar toweekEnd = Calendar.getInstance();
        int day_of_week = toweekEnd.get(Calendar. DAY_OF_WEEK) - 1;
        if (day_of_week == 0 ) {
            day_of_week = 7 ;
        }
        toweekEnd.add(Calendar.DATE , -day_of_week + 7 );
        //toweekEnd.add(Calendar.DATE, 6);
        toweekEnd.set(Calendar.HOUR_OF_DAY, 23);
        toweekEnd.set(Calendar.MINUTE, 59);
        toweekEnd.set(Calendar.SECOND, 59);
        toweekEnd.set(Calendar.MILLISECOND,999);
        return toweekEnd.getTime();
    }

    /**
     * 获得本月开始时间
     * */
    public static Date getMonthStartDay() {
        Calendar tomonthStart = Calendar.getInstance();
        tomonthStart.add(Calendar.MONTH, 0);
        tomonthStart.set(tomonthStart.get(Calendar.YEAR), tomonthStart.get(Calendar.MONTH), tomonthStart.getActualMinimum(Calendar.DAY_OF_MONTH));
        tomonthStart.set(Calendar.HOUR_OF_DAY, 0);
        tomonthStart.set(Calendar.MINUTE, 0);
        tomonthStart.set(Calendar.SECOND, 0);
        tomonthStart.set(Calendar.MILLISECOND,0);
        return tomonthStart.getTime();
    }

    /**
     * 获得本月结束时间
     * */
    public static Date getMonthEndDay() {
        Calendar tomonthEnd = Calendar.getInstance();
        tomonthEnd.add(Calendar.MONTH, 0);
        tomonthEnd.set(tomonthEnd.get(Calendar.YEAR), tomonthEnd.get(Calendar.MONTH), tomonthEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
        tomonthEnd.set(Calendar.HOUR_OF_DAY, 23);
        tomonthEnd.set(Calendar.MINUTE, 59);
        tomonthEnd.set(Calendar.SECOND, 59);
        tomonthEnd.set(Calendar.MILLISECOND,999);
        return tomonthEnd.getTime();
    }



    public static String dateToString(Date date) {
        return date == null ? "" : dateToString(date, "yyyy-MM-dd");
    }

    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 转换特殊时间格式
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static String starttime(String dateStr) throws ParseException {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
        Date result;
        result = df.parse(dateStr);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(result);
    }


}
